package model.impl;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import model.bo.News;
import model.dao.INewsDAO;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.context.ApplicationContext;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/**
 * Data access object (DAO) for domain model class News.
 * 
 * @see model.bo.News
 * @author MyEclipse Persistence Tools
 */

public class NewsDAO extends HibernateDaoSupport implements INewsDAO {
	private static final Log log = LogFactory.getLog(NewsDAO.class);

	protected void initDao() {
		// do nothing
	}

	/* (non-Javadoc)
	 * @see model.impl.INewsDAO#save(model.bo.News)
	 */
	public void save(News transientInstance) {
		log.debug("saving News instance");
		try {
			getHibernateTemplate().saveOrUpdate(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see model.impl.INewsDAO#delete(model.bo.News)
	 */
	public void delete(News persistentInstance) {
		log.debug("deleting News instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see model.impl.INewsDAO#findById(java.lang.Integer)
	 */
	public News findById(java.lang.Integer id) {
		log.debug("getting News instance with id: " + id);
		try {
			News instance = (News) getHibernateTemplate().get("model.bo.News",
					id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see model.impl.INewsDAO#findByExample(model.bo.News)
	 */
	public List findByExample(News instance) {
		log.debug("finding News instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see model.impl.INewsDAO#findByProperty(java.lang.String, java.lang.Object)
	 */
	public List findByProperty(String propertyName, Object value) {
		log.debug("finding News instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from News as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see model.impl.INewsDAO#findByTitle(java.lang.Object)
	 */
	public List findByTitle(Object title) {
		return findByProperty(TITLE, title);
	}

	/* (non-Javadoc)
	 * @see model.impl.INewsDAO#findByBody(java.lang.Object)
	 */
	public List findByBody(Object body) {
		return findByProperty(BODY, body);
	}

	/* (non-Javadoc)
	 * @see model.impl.INewsDAO#findAll()
	 */
	public List findAll() {
		log.debug("finding all News instances");
		try {
			String queryString = "from News";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see model.impl.INewsDAO#merge(model.bo.News)
	 */
	public News merge(News detachedInstance) {
		log.debug("merging News instance");
		try {
			News result = (News) getHibernateTemplate().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see model.impl.INewsDAO#attachDirty(model.bo.News)
	 */
	public void attachDirty(News instance) {
		log.debug("attaching dirty News instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see model.impl.INewsDAO#attachClean(model.bo.News)
	 */
	public void attachClean(News instance) {
		log.debug("attaching clean News instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static INewsDAO getFromApplicationContext(ApplicationContext ctx) {
		return (INewsDAO) ctx.getBean("NewsDAO");
	}
	
	
	//SQL回调
	public int execusql(String sql) {
		Session session = this.getSession();
		Transaction tx = session.beginTransaction();
		int result = -1;
		try {
			tx.begin();
			result = session.createSQLQuery(sql).executeUpdate();
			session.flush();
			tx.commit();
		} catch (DataAccessException e) {
			e.printStackTrace();
			if (tx != null) {
				tx.rollback();
			}
		} finally {
			session.close();
		}
		return result;
	}

	//通过HQL返回查询结果总数
	public int getAllRowCount(String hql) {
		return getHibernateTemplate().find(hql).size();
	}

	//通过HQL查询,带分页.
	public List queryForPage(final String hql, final int offset,
			final int length) {
		List list = getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(hql);
				query.setFirstResult(offset);
				query.setMaxResults(length);
				List list = query.list();
				return list;
			}
		});
		return list;
	}
}